package com.demo.controller;

import com.demo.controller.base.BaseCtrl;
import com.demo.controller.model.RSResult;
import com.demo.po.GroupPo;
import com.demo.service.IGroupService;
import com.demo.service.common.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/group")
public class GroupController extends BaseCtrl {

    private final IGroupService groupService;

    @Autowired
    public GroupController(IGroupService groupService) {
        this.groupService = groupService;
    }

    @PostMapping(value = "/list")
    @ResponseBody
    public RSResult list(@RequestBody GroupPo group) {
        log.info("Start get list of the table : group, the request is : {}", group);
        RSResult result = new RSResult();
        try {
            int count = groupService.countGroups(group);
            List<GroupPo> list = new ArrayList<>();
            if (count > 0) {
                group.setPagestart((group.getCurpage() - 1) * group.getPagesize());
                list = groupService.findGroupsByPage(group);
            }
            result.setResult(0);
            result.setTotal(count);
            result.setData(list);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Get list of the table : group, success.");
        return result;
    }

    @PostMapping(value = "/detail")
    @ResponseBody
    public RSResult info(@RequestBody GroupPo group) {
        log.info("Get detail object of the table : group, the request is : {}", group);
        RSResult result = new RSResult();
        try {
            GroupPo obj = groupService.getGroup(group.getGroupid());
            result.setResult(0);
            result.setData(obj);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Get detail object of the table : group, success.");
        return result;
    }

    @PostMapping(value = "/new")
    @ResponseBody
    public RSResult add(@RequestBody GroupPo group) {
        log.info("Add object to the table : group, the request is : {}", group);
        RSResult result = new RSResult();
        try {
            Integer id = groupService.addGroup(group);
            result.setResult(0);
            result.setData(id);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Add object to the table : group, success.");
        return result;
    }

    @PostMapping(value = "/update")
    @ResponseBody
    public RSResult update(@RequestBody GroupPo group) {
        log.info("Update object from the table : group, the request is : {}", group);
        RSResult result = new RSResult();
        try {
            groupService.updateGroup(group);
            result.setResult(0);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Update object from the table : group, success.");
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public RSResult delete(@RequestBody GroupPo group) {
        log.info("Delete object from the table : group, the request is : {}", group);
        RSResult result = new RSResult();
        try {
            groupService.deleteGroup(group.getGroupid());
            result.setResult(0);
        } catch (BizException ex) {
            log.error("{} : {}", ex.getErrorcode(), ex.getRemark());
            result.setResult(ex.getErrorcode(), ex.getRemark());
        }
        log.info("Delete object from the table : group, success.");
        return result;
    }
}
